﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using Newtonsoft.Json;
using SZHL.Data;
namespace SZHL.UI.AppCode
{

    #region 业务模块
    public class ZH_CCXJB : BaseEFDao<ZH_CCXJ>
    {
        /// <summary>
        /// 审批请假数据
        /// </summary>
        /// <param name="strFormID"></param>
        /// <param name="strUserName"></param>
        /// <param name="strSTATUS"></param>
        /// <param name="strContent"></param>
        public void SPData(string strFormID, string strUserName, string strSTATUS, string strContent)
        {
            ZH_CCXJ MODEL = new ZH_CCXJB().GetEntity(D => D.ID.ToString() == strFormID);
            MODEL.SPStatus = strSTATUS;
            MODEL.SPTime = DateTime.Now;
            MODEL.SPUser = strUserName;
            MODEL.SPYJ = strContent;
            new ZH_CCXJB().Update(MODEL);
        }

    }


    public class ZH_GZRZB : BaseEFDao<ZH_GZRZ>
    {
        /// <summary>
        /// 获取工作日志列表
        /// </summary>
        /// <param name="type">工作日志类型</param>
        /// <param name="CRUser">创建人</param>
        /// <param name="searchFilter">查询条件</param>
        /// <param name="branchName">部门</param>
        /// <param name="page">页码</param>
        /// <returns></returns>
        public DataTable GETGZRZList(string type, string CRUser, string searchFilter, string branchName, int page)
        {

            string strWhere = " 1=1 ";
            if (type != "")
            {
                strWhere += string.Format(" And  remark='{0}'", type);
            }
            if (CRUser != "")
            {
                strWhere += string.Format(" And  CRUser='{0}'", CRUser);
            }

            if (searchFilter != "")
            {
                strWhere += "And ( ";
                if (CRUser == "") //如果是我的日报菜单查询条件中不包含用户名
                {
                    strWhere += string.Format("  CRUserName like '%{0}%' or ", searchFilter);
                }
                strWhere += string.Format("  RBContent like '%{0}%')", searchFilter);
            }
            if (branchName != "")
            {
                strWhere += string.Format(" And  BranchName='{0}'", branchName);
            }

            // string strSQL = string.Format("select * from (select  nID = row_number() over(order by CRDate DESC), rz.*,u.UserRealName,DATEDIFF(day,rz.CRDate,GETDATE()) days from ZH_GZRZ rz inner join JH_Auth_User u  on  rz.CRUser=u.UserName   where 1=1  {0}) as newTab WHERE newTab.nID <={1}  ", strWhere, page * 5);
            // DataTable dt = new ZH_XTGGB().GetDTByCommand(strSQL);
            int total = 0;
            DataTable dt = new ZH_GZRZB().GetDataPager("ZH_GZRZ", "BranchName,RBContent,RBJSR,RBWCQK,RBDate,Remark,CRUser,CRUserName,CRDate,ID,Files  ", 8, page, "CRDate desc", strWhere, ref total);
            dt.Columns.Add("PLList", Type.GetType("System.Object"));
            dt.Columns.Add("FileList", Type.GetType("System.Object"));
            foreach (DataRow row in dt.Rows)
            {
                row["PLList"] = new ZH_GZRZB().GetDTByCommand("  SELECT ID,MSGTLYID,MSGType,MSGContent,CRDate,CRUser,CRUserName  FROM JH_Auth_TL WHERE MSGType='GZRZ' AND  MSGTLYID='" + row["ID"] + "'");
                string[] fileIds = row["Files"].ToString().Split(',');
                row["FileList"] = new FT_FileB().GetEntities(d => fileIds.Contains(d.ID.ToString()));
            }
            return dt;
        }
        /// <summary>
        /// 获取所有日报列表
        /// </summary>
        /// <param name="type">日报类型</param>
        /// <param name="searchFilter">日报查询条件</param>
        /// <returns></returns>
        public DataTable GETALLGZRZList(string type, string searchFilter)
        {

            string strWhere = " ";
            if (type != "")
            {
                strWhere += string.Format(" And  remark='{0}'", type);
            }
            if (searchFilter != "")
            {
                strWhere += "And ( ";

                strWhere += string.Format("  CRUserName like '%{0}%' or ", searchFilter);
                strWhere += string.Format("  RBContent like '%{0}%')", searchFilter);
            }
            string strSql = string.Format("select  BranchName,CONVERT(VARCHAR(10), RBDate,120) RBDate,Remark,CRUserName,RBContent,RBWCQK,ID  from ZH_GZRZ      where 1=1 " + strWhere + " order by CRDate desc");
            return new ZH_GZRZB().GetDTByCommand(strSql);
        }

    }

    public class ZH_JFBXB : BaseEFDao<ZH_JFBX>
    {
        /// <summary>
        /// 审批请假数据
        /// </summary>
        /// <param name="strFormID"></param>
        /// <param name="strUserName"></param>
        /// <param name="strSTATUS"></param>
        /// <param name="strContent"></param>
        public void SPData(string strFormID, string strUserName, string strSTATUS, string strContent)
        {
            ZH_JFBX MODEL = new ZH_JFBXB().GetEntity(D => D.ID.ToString() == strFormID);
            MODEL.SPStatus = strSTATUS;
            MODEL.SPTime = DateTime.Now;
            MODEL.SPUser = strUserName;
            MODEL.SPYJ = strContent;
            new ZH_JFBXB().Update(MODEL);
        }
        /// <summary>
        /// 获取编号
        /// </summary>
        /// <param name="tableName">表名称</param>
        public string GetFormCode(string tableName)
        {
            string strSql = string.Format("select top 1 formcode from {0} where CRDate>'{1}' order by CRDate DESC", tableName, DateTime.Now.ToShortDateString());
            object obj = new ZH_JFBXB().ExsSclarSql(strSql);
            string formCode = DateTime.Now.ToString("yyyyMMdd");
            if (obj == null || obj.ToString() == "")
            {
                formCode = formCode + "001";
            }
            else
            {
                string preFormCode = obj.ToString();
                int code = 0;
                int.TryParse(preFormCode.Substring(preFormCode.Length - 3), out code);
                formCode = formCode + (code + 1).ToString("000");
            }
            return formCode;
        }
    }

    public class ZH_LCSPB : BaseEFDao<ZH_LCSP>
    { }
    public class ZH_JFBX_ItemB : BaseEFDao<ZH_JFBX_Item>
    { }


    public class ZH_KHGLB : BaseEFDao<ZH_KHGL>
    {

    }

    public class ZH_HDBMB : BaseEFDao<ZH_HDBM>
    {

    }

    public class ZH_HDBM_ItemB : BaseEFDao<ZH_HDBM_Item>
    {

    }
    public class ZH_RCTXB : BaseEFDao<ZH_RCTX>
    {

    }

    public class JH_Auth_TLB : BaseEFDao<JH_Auth_TL>
    {
        public DataTable GetTL(string strMsgType, string MSGTLYID)
        {
            DataTable dtList = new DataTable();
            dtList = new ZH_RCTXB().GetDTByCommand("  SELECT ID,MSGTLYID,MSGType,MSGContent,CRDate,CRUser,CRUserName  FROM JH_Auth_TL WHERE MSGType='" + strMsgType + "' AND  MSGTLYID='" + MSGTLYID + "'");
            return dtList;
        }
    }
    public class JH_Auth_QYB : BaseEFDao<JH_Auth_QY>
    {

    }
    public class JH_Auth_User_CenterB : BaseEFDao<JH_Auth_User_Center>
    {

    }



    public class ZH_DXGLB : BaseEFDao<ZH_DXGL>
    {

    }

    #endregion



    #region 扩展模块
    public class KZ_HTGLB : BaseEFDao<KZ_HTGL>
    {

    }
    public class KZ_HTGL_ZFJLB : BaseEFDao<KZ_HTGL_ZFJL>
    {

    }
    public class KZ_KJGLB : BaseEFDao<KZ_KJGL>
    {

    }
    public class KZ_XMGLB : BaseEFDao<KZ_XMGL>
    {

    }

    public class KZ_UserInfoB : BaseEFDao<KZ_UserInfo>
    {

    }
    public class KZ_LSGLB : BaseEFDao<KZ_LSGL>
    {

    }
    #endregion


    #region 文档管理模块
    public class FT_FolderB : BaseEFDao<FT_Folder>
    {


        public FoldFile GetWDTREE(int FolderID, ref List<FoldFileItem> ListID)
        {
            List<FT_Folder> ListAll = new FT_FolderB().GetEntities().ToList();
            FT_Folder Folder = new FT_FolderB().GetEntity(d => d.ID == FolderID);
            FT_FolderB.FoldFile Model = new FT_FolderB.FoldFile();
            Model.Name = Folder.Name;
            Model.FolderID = Folder.ID;

            ListID.Add(new FoldFileItem() { ID = Folder.ID, Type = "folder" });
            Model.SubFileS = new FT_FileB().GetEntities(d => d.FolderID == Folder.ID).ToList();

            foreach (var item in Model.SubFileS)
            {
                ListID.Add(new FoldFileItem() { ID = item.ID, Type = "file" });

            }
            Model.SubFolder = new FT_FolderB().GETNEXTFLODER(Folder.ID, ListAll, ref ListID);
            return Model;
        }


        private List<FoldFile> GETNEXTFLODER(int FolderID, List<FT_Folder> ListAll, ref List<FoldFileItem> ListID)
        {
            List<FoldFile> ListData = new List<FoldFile>();
            var list = ListAll.Where(d => d.PFolderID == FolderID);
            foreach (var item in list)
            {
                FoldFile FolderNew = new FoldFile();
                FolderNew.FolderID = item.ID;
                FolderNew.Name = item.Name;
                FolderNew.SubFileS = new FT_FileB().GetEntities(d => d.FolderID == item.ID).ToList();
                foreach (var SubFile in FolderNew.SubFileS)
                {
                    ListID.Add(new FoldFileItem() { ID = SubFile.ID, Type = "file" });
                }
                FolderNew.SubFolder = GETNEXTFLODER(item.ID, ListAll, ref ListID);
                ListData.Add(FolderNew);
                ListID.Add(new FoldFileItem() { ID = item.ID, Type = "folder" });
            }
            return ListData;

        }


        public void CopyFloderTree(int FloderID, int PID)
        {
            List<FoldFileItem> ListID = new List<FoldFileItem>();
            FoldFile Model = new FT_FolderB().GetWDTREE(FloderID, ref ListID);
            FT_Folder Folder = new FT_FolderB().GetEntity(d => d.ID == Model.FolderID);
            Folder.PFolderID = PID;
            new FT_FolderB().Insert(Folder);
            foreach (FT_File file in Model.SubFileS)
            {
                file.FolderID = Folder.ID;
                new FT_FileB().Insert(file);
            }
            GreateWDTree(Model.SubFolder, Folder.ID);
        }

        /// <summary>
        /// 根据父ID创建树装结构文档
        /// </summary>
        /// <param name="ListFoldFile"></param>
        private void GreateWDTree(List<FoldFile> ListFoldFile, int newfolderid)
        {

            foreach (FoldFile item in ListFoldFile)
            {

                FT_Folder PModel = new FT_FolderB().GetEntity(d => d.ID == item.FolderID);
                PModel.PFolderID = newfolderid;
                new FT_FolderB().Insert(PModel);
                foreach (FT_File file in item.SubFileS)
                {
                    file.FolderID = PModel.ID;
                    new FT_FileB().Insert(file);
                }

                GreateWDTree(item.SubFolder, PModel.ID);

         

            }
        }



        public void DelWDTree(int FolderID)
        {
            List<FoldFileItem> ListID = new List<FoldFileItem>();
            new FT_FolderB().GetWDTREE(FolderID, ref ListID);
            foreach (FoldFileItem listitem in ListID)
            {
                if (listitem.Type == "file")
                {
                    new FT_FileB().Delete(d => d.ID == listitem.ID);
                }
                else
                {
                    new FT_FolderB().Delete(d => d.ID == listitem.ID);
                }
            }

        }



        public class FoldFile
        {
            public int FolderID { get; set; }
            public string Name { get; set; }

            public List<FoldFile> SubFolder { get; set; }
            public List<FT_File> SubFileS { get; set; }

        }
        public class FoldFileItem
        {
            public int ID { get; set; }
            public string Type { get; set; }

        }
    }
    public class FT_FileB : BaseEFDao<FT_File>
    {
        //public FT_File GetFileByMD5(string strFormID)
        //{
        //    FT_File MODEL = new FT_File().GetEntity(D => D.ID.ToString() == strFormID);
        //    MODEL.SPStatus = strSTATUS;
        //    MODEL.SPTime = DateTime.Now;
        //    MODEL.SPUser = strUserName;
        //    MODEL.SPYJ = strContent;
        //    new ZH_CCXJB().Update(MODEL);
        //}


    }

    public class FT_File_CollectB : BaseEFDao<FT_File_Collect>
    {

    }

    public class FT_File_DownhistoryB : BaseEFDao<FT_File_Downhistory>
    {

    }


    public class FT_File_ShareB : BaseEFDao<FT_File_Share>
    {

    }


    public class FT_File_UserAuthB : BaseEFDao<FT_File_UserAuth>
    {

    }


    public class FT_File_UserTagB : BaseEFDao<FT_File_UserTag>
    {

    }

    public class FT_File_VesionB : BaseEFDao<FT_File_Vesion>
    {

    }
    #endregion

}